package com.test.controller;


import java.util.HashMap;
import java.util.Map;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.comment.FightBean;
import com.test.comment.SingerBean;
import com.test.comment.UserBean;
import com.test.services.VoteServices;


@Controller

@RequestMapping("/vote")
public class VoteController {
	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	public VoteServices voteServices;

	@RequestMapping("/login")
	public ModelAndView Login() {
		return new ModelAndView("vote");
	}
	
	@RequestMapping("/userinfor")
	public UserBean userinfor(HttpSession session){
		UserBean userBean = (UserBean)session.getAttribute("user");
		return userBean;
	}
	@ResponseBody
	//投票界面信息显示
	@RequestMapping("/SV")
	public Map<String, Object> SelectVote() {
		System.out.println("====获取比赛id====");
		FightBean fight = voteServices.FindFight();
		int rid=fight.getRid();
		System.out.println("====获取比赛id成功====");
		
		System.out.println("====获取歌手id====");
		int singer1 = fight.getSidOne();
		int singer2 = fight.getSidTwo();
		System.out.println("====获取歌手id成功====");
		
		System.out.println("====获取歌手信息====");
		SingerBean singerBean1=voteServices.findSinger(singer1);
		SingerBean singerBean2=voteServices.findSinger(singer2);
		
		String SI1=singerBean1.getSingerImg();//歌手1的头像
		String SN1=singerBean1.getSingerName();//歌手1的名称
		
		String SI2=singerBean2.getSingerImg();//歌手2的头像
		String SN2=singerBean2.getSingerName();//歌手2的名称
		System.out.println("====获取歌手信息成功====");
		
		System.out.println("====开始信息整理====");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rid", rid);
		map.put("sid1", singer1);
		map.put("SI1", SI1);
		map.put("SN1", SN1);
		map.put("sid2", singer2);
		map.put("SI2", SI2);
		map.put("SN2", SN2);
		System.out.println("====信息整理完成====");
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/voting")
	public Map<String, String> Voting(HttpSession session,int rid,int sid){
		UserBean userBean = (UserBean)session.getAttribute("user");
		logger.debug("=====================usercode:"+userBean.getUsercode());
		logger.debug("=============="+userBean.getUsercode());
		logger.debug("=============="+rid);
		logger.debug("=============="+sid);
		System.out.println("====开始=====");
		String a = voteServices.Selectuservote(userBean.getUsercode(),rid,sid);
		System.out.println("====开始投票=====");
		Map<String , String> map =new HashMap<String, String>();
		if(a.equals("1")) {
			System.out.println("已投票");
			map.put("msg", "voted");
			return map;
		}else{
			System.out.println("投票成功");
			map.put("msg", "success");
			return map;
		}
		
	}
	
	
}
